// Nick Nissen, Mads Jakobsen og Jesper Larsen
package hc.andersen.paraden;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;


public class ProfileActivity extends Activity {

    String sogeid;
    String tlfNr;
    String kontaktNr;
    String mobilNr;
    String billedURL;
    String navn;
    String fødselsdag;
    String adresse;
    ImageView profilpic;

    JSONObject profil;
    JSONArray rolleArr;
    JSONObject HentProfilResult;
    HashMap<Integer, Rolle> rolleHashMap;
    Rolle rolleObj;

    LinearLayout rolleLinearLayout;

    @Override
    public void onCreate(Bundle savedInstanceState) {



        super.onCreate(savedInstanceState);
        setContentView(R.layout.profile);



        try {


            //MADSMADSMADS
            Bundle bundle = this.getIntent().getExtras();
            if (bundle != null) {
                sogeid = bundle.getString("id");
            }
            //----------------------

            HentProfilResult = DataHelper.getHttpGetRequest("http://epanum.dk/Teater/ProfilService.svc/Profil/" + sogeid);

            // grabbing the profil objektet
            profil = HentProfilResult.getJSONObject("HentProfilResult");
            billedURL = profil.getString("BilledeUrl");
            navn = profil.getString("Navn");
            fødselsdag = profil.getString("Fødselsdag");
            adresse = profil.getString("Addresse");
            tlfNr = profil.getString("TlfNr");
            kontaktNr = profil.getString("KontaktNr");
            mobilNr = profil.getString("MobilNr");

            rolleArr = profil.getJSONArray("Roller");

            rolleHashMap = new HashMap<Integer, Rolle>();

            for (int i = 0; i < rolleArr.length(); i++) {
                JSONObject rolle = rolleArr.getJSONObject(i);

                Integer rolleid = Integer.parseInt(rolle.getString("RolleId"));
                String rollenavn = rolle.getString("Navn");

                rolleObj = new Rolle(rolleid, rollenavn, rolle.getString("Sprog"));

                rolleHashMap.put(i, rolleObj);

            }

            profilpic = (ImageView) findViewById(R.id.profile_pic);
            if(LoadImageFromWebOperations(billedURL) != null){
                Drawable drawable = LoadImageFromWebOperations(billedURL);
                profilpic.setBackgroundDrawable(drawable);
            }

            //opdatere textview
            TextView tv = (TextView) findViewById(R.id.txtNavn);
            tv.setText(navn);

            tv = (TextView) findViewById(R.id.txtFoedselsdag);
            tv.setText(fødselsdag);

            tv = (TextView) findViewById(R.id.txtAdresse);
            tv.setText(adresse);

            tv = (TextView) findViewById(R.id.txtHjem);
            tv.setText(tlfNr + "- klik for at ringe op");

            tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Uri number = Uri.parse("tel:" + tlfNr);
                    Intent opkald = new Intent(Intent.ACTION_DIAL, number);
                    startActivity(opkald);

                }
            });

            tv = (TextView) findViewById(R.id.txtMobil);
            tv.setText(mobilNr + "- klik for at ringe op");
            tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Uri number = Uri.parse("tel:" + mobilNr);
                    Intent opkald = new Intent(Intent.ACTION_DIAL, number);
                    startActivity(opkald);

                }
            });

            tv = (TextView) findViewById(R.id.txtKontakt);
            tv.setText(kontaktNr + "- klik for at ringe op");
            tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Uri number = Uri.parse("tel:" + kontaktNr);
                    Intent opkald = new Intent(Intent.ACTION_DIAL, number);
                    startActivity(opkald);

                }
            });

            rolleLinearLayout = (LinearLayout) findViewById(R.id.roller);

            final int N = rolleHashMap.size(); // total number of textviews to add

            final TextView[] myTextViews = new TextView[N]; // create an empty array;

            for (int i = 1; i < N; i++) {
                final TextView rowTextView = new TextView(this);

                String sprog = "";
                if (rolleHashMap.get(i).getSprog().equals("en")) {
                    sprog = "Engelsk";
                } else if (rolleHashMap.get(i).getSprog().equals("de")) {
                    sprog = "Tysk";
                } else {
                    sprog = "Dansk";
                }

                rowTextView.setText(rolleHashMap.get(i).getNavn() + "         " + sprog);
                rowTextView.setTextAppearance(this, R.style.InfoTextView);

                // add the textview to the linearlayout
                rolleLinearLayout.addView(rowTextView);

                // save a reference to the textview for later
                myTextViews[i] = rowTextView;

                if(DataHelper.getDialog() != null && DataHelper.getDialog().isShowing())
                    DataHelper.getDialog().dismiss();

            }

        } catch (Exception e) {

            Toast.makeText(this, "Fejl, kunne ikke indlæse data" + e.getMessage(), Toast.LENGTH_LONG);
        }
    }

    //MADS
    private Drawable LoadImageFromWebOperations(String url) {

        try {
            InputStream is = (InputStream) new URL(url).getContent();
            Drawable d = Drawable.createFromStream(is, "src name");
            return d;
        } catch (Exception e) {
            return null;
        }

    }
}

class Rolle {

    private Integer rolleId;
    private String navn;
    private String sprog;

    Rolle(Integer rolleId, String navn, String sprog) {
        this.rolleId = rolleId;
        this.navn = navn;
        this.sprog = sprog;
    }

    public Integer getRolleId() {
        return rolleId;
    }

    public void setRolleId(Integer rolleId) {
        this.rolleId = rolleId;
    }

    public String getNavn() {
        return navn;
    }

    public void setNavn(String navn) {
        this.navn = navn;
    }

    public String getSprog() {
        return sprog;
    }

    public void setSprog(String sprog) {
        this.sprog = sprog;
    }


}
